#include "etcd.hpp"
#include "channel.hpp"
#include "utils.hpp"
#include <gflags/gflags.h>
#include <thread>
#include <gtest/gtest.h>
#include "transmite.pb.h"

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(transmite_service, "/service/transmite_service", "服务监控根目录");

zjx_im::ServiceManager::ptr sm;

void string_message(const std::string &uid, const std::string &sid, const std::string &msg)
{
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        return;
    }
    // 4. 发起EchoRpc调用
    zjx_im::MsgTransmitService_Stub stub(channel.get());
    zjx_im::NewMessageReq req;
    zjx_im::GetTransmitTargetRsp rsp;
    req.set_request_id(zjx_im::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(zjx_im::STRING);
    req.mutable_message()->mutable_string_message()->set_content(msg);

    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void image_message(const std::string &uid, const std::string &sid, const std::string &msg)
{
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        return;
    }
    // 4. 发起EchoRpc调用
    zjx_im::MsgTransmitService_Stub stub(channel.get());
    zjx_im::NewMessageReq req;
    zjx_im::GetTransmitTargetRsp rsp;
    req.set_request_id(zjx_im::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(zjx_im::IMAGE);
    req.mutable_message()->mutable_image_message()->set_image_content(msg);

    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void speech_message(const std::string &uid, const std::string &sid, const std::string &msg)
{
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        return;
    }
    // 4. 发起EchoRpc调用
    zjx_im::MsgTransmitService_Stub stub(channel.get());
    zjx_im::NewMessageReq req;
    zjx_im::GetTransmitTargetRsp rsp;
    req.set_request_id(zjx_im::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(zjx_im::SPEECH);
    req.mutable_message()->mutable_speech_message()->set_file_contents(msg);

    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void file_message(const std::string &uid, const std::string &sid, const std::string &filename, const std::string &content)
{
    auto channel = sm->choose(FLAGS_transmite_service);
    if (!channel)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        return;
    }
    // 4. 发起EchoRpc调用
    zjx_im::MsgTransmitService_Stub stub(channel.get());
    zjx_im::NewMessageReq req;
    zjx_im::GetTransmitTargetRsp rsp;
    req.set_request_id(zjx_im::uuid());
    req.set_user_id(uid);
    req.set_chat_session_id(sid);
    req.mutable_message()->set_message_type(zjx_im::FILE);
    req.mutable_message()->mutable_file_message()->set_file_contents(content);
    req.mutable_message()->mutable_file_message()->set_file_name(filename);
    req.mutable_message()->mutable_file_message()->set_file_size(content.size());

    brpc::Controller cntl;
    stub.GetTransmitTarget(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    zjx_im::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    // 1. 先构造Rpc信道管理对象
    sm = std::make_shared<zjx_im::ServiceManager>();
    sm->declared(FLAGS_transmite_service);
    auto put_cb = std::bind(&zjx_im::ServiceManager::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&zjx_im::ServiceManager::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
    // 2. 构造服务发现对象
    zjx_im::Discovery::ptr dclient = std::make_shared<zjx_im::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);

    // 循环调用
    // 3. 通过Rpc信道管理对象，获取提供服务的信道
    string_message("06da-1f9dfed5-0000", "会话ID1", "吃饭了吗");
    string_message("f827-020a969f-0001", "会话ID1", "吃的小汉堡");

    image_message("06da-1f9dfed5-0000", "会话ID1", "表情图片");

    speech_message("06da-1f9dfed5-0000", "会话ID1", "动听的声音");

    file_message("06da-1f9dfed5-0000", "会话ID1", "猪爸爸的文件名称", "猪猪文件数据");
    return 0;
}